import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { createStore } from 'redux';
import Counter from './components/Counter';
import { Provider } from 'react-redux'


/**
 * react - redux
 * Provider // 将store放在一个全局的，组件能够访问到的
 * connect
 */


const initialState = {
  count: 0
} 

function reducer (state = initialState, action) {
  switch(action.type) {
    case 'increment':
      return {
        count: state.count + 1
      }
    case 'decrement': 
      return {
        count: state.count - 1
      }
    default:
      return state
  }
}

const store = createStore(reducer)

const increment = { type: 'increment' }
const decrement = { type: 'decrement' }

store.subscribe(() => {
  ReactDOM.render(
    <Counter />,
    document.getElementById('root')
  );
})

ReactDOM.render(
  // 通过provider组件， 将store 放在全局的组件可以够的到的地方
  <Provider store = { store }><Counter /></Provider>,
  document.getElementById('root')
);
